//
//  EditView.m
//  dhfsAPP
//
//  Created by Devil on 2017/8/2.
//  Copyright © 2017年 Devil. All rights reserved.
//

#import "EditView.h"
@interface EditView()



//单选按钮,保存按钮
@property(strong,nonatomic)UIButton *maleBtn,*femaleBtn,*saveBtn;

@end

@implementation EditView
#pragma mark - 懒加载
-(HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(128)) Title:@"资料编辑"];
    }
    
    return _headerView;
}

-(UIButton *)headerImgBtn{
    if (!_headerImgBtn) {
        _headerImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_headerImgBtn setBackgroundImage:[UIImage imageNamed:@"添加照片2"] forState:UIControlStateNormal];
        
        _headerImgBtn.frame = CGRectMake(0, HEIGHT(174), WIDTH(130), HEIGHT(130));
        
        _headerImgBtn.center = CGPointMake(self.center.x, _headerImgBtn.center.y);
        
        _headerImgBtn.tag = EditViewButtonTypeHeaderImg;
        
        [_headerImgBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        _headerImgBtn.layer.masksToBounds = YES;
        _headerImgBtn.layer.cornerRadius = WIDTH(65);
    }
    
    return _headerImgBtn;
}

-(UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(WIDTH(126), HEIGHT(360), WIDTH(579), HEIGHT(50))];
        
        _nameTextField.placeholder = @"";
        
        _nameTextField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
        
    }
    
    return _nameTextField;
}

-(UIButton *)femaleBtn{
    if (!_femaleBtn) {
        _femaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _femaleBtn.frame = CGRectMake(WIDTH(128), HEIGHT(457), WIDTH(32), HEIGHT(32));
        
        [_femaleBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
        [_femaleBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
        
        self.lastBtn = _femaleBtn;
        _femaleBtn.selected = YES;
        
        _femaleBtn.tag = EditViewButtonTypeFemale;
        [_femaleBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _femaleBtn;
}

-(UIButton *)maleBtn{
    if (!_maleBtn) {
        _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _maleBtn.frame = CGRectMake(WIDTH(242), HEIGHT(457), WIDTH(32), HEIGHT(32));
        
        [_maleBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
        [_maleBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
        
        _maleBtn.tag = EditViewButtonTypeMale;
        [_maleBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _maleBtn;
}

-(UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        
        [_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _saveBtn.frame = CGRectMake(WIDTH(44), HEIGHT(1122), WIDTH(660), HEIGHT(88));
        
        _saveBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        _saveBtn.backgroundColor = RGB(0, 105, 193);
        
        _saveBtn.layer.masksToBounds = YES;
        _saveBtn.layer.cornerRadius = 10.0;
        
        _saveBtn.tag = EditViewButtonTypeSave;
        [_saveBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _saveBtn;
}
#pragma mark - 构造函数
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self initUI];
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame UserInfo:(NSDictionary *)userInfo{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self initUI];
        self.userInfo = userInfo;
        
        [self.headerImgBtn setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",userInfo[@"img"]]]]] forState:UIControlStateNormal];
        
        self.nameTextField.placeholder = userInfo[@"name"];
        
        if ([[NSString stringWithFormat:@"%@",userInfo[@"sex"]] isEqualToString:@"男"]) {
            self.lastBtn.selected = NO;
            
            self.maleBtn.selected = YES;
            
            self.lastBtn = self.maleBtn;
        }
    }
    
    return self;
}
#pragma mark - 其它函数
-(void)initUI{
    [self addSubview:self.headerView];
    
    [self addSubview:self.headerImgBtn];
    
    UIView *iView = [[UIView alloc] initWithFrame:self.headerImgBtn.frame];
    
    iView.backgroundColor = [UIColor clearColor];
    
    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"添加照片"]];
    
    imgV.frame = CGRectMake(WIDTH(91), HEIGHT(92), WIDTH(39), HEIGHT(38));
    
    [iView addSubview:imgV];
    
    [self addSubview:iView];
    
    [self bringSubviewToFront:iView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImg)];
    
    iView.userInteractionEnabled = YES;
    [iView addGestureRecognizer:tap];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(47), 0, 0, 0)];
    
    nameLabel.text = @"昵称";
    nameLabel.textColor = RGB(51, 51, 51);
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
    
    [nameLabel sizeToFit];
    
    nameLabel.center = CGPointMake(nameLabel.center.x, self.nameTextField.center.y);
    
    [self addSubview:nameLabel];
    
    [self addSubview:self.nameTextField];
    
    UILabel *sexLabel = [[UILabel alloc] init];
    
    sexLabel.text = @"性别";
    sexLabel.textColor = RGB(51, 51, 51);
    sexLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
    
    [sexLabel sizeToFit];
    
    sexLabel.center = CGPointMake(nameLabel.center.x, self.maleBtn.center.y);
    
    [self addSubview:sexLabel];
    
    UILabel *femaleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(169), 0, 0, 0)];
    
    femaleLabel.text = @"女";
    femaleLabel.textColor = RGB(51, 51, 51);
    femaleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];

    [femaleLabel sizeToFit];
    femaleLabel.center = CGPointMake(femaleLabel.center.x, sexLabel.center.y);
    
    [self addSubview:femaleLabel];
    
    UILabel *maleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(285), 0, 0, 0)];
    
    maleLabel.text = @"男";
    maleLabel.textColor = RGB(51, 51, 51);
    maleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
    
    [maleLabel sizeToFit];
    maleLabel.center = CGPointMake(maleLabel.center.x, sexLabel.center.y);
    
    [self addSubview:maleLabel];
    
    [self addSubview:self.maleBtn];
    [self addSubview:self.femaleBtn];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(WIDTH(46), HEIGHT(427), WIDTH(660), HEIGHT(2))];
    
    lineView1.backgroundColor = RGB(221, 221, 221);
    
    [self addSubview:lineView1];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(WIDTH(46), HEIGHT(517), WIDTH(660), HEIGHT(2))];
    
    lineView2.backgroundColor = RGB(221, 221, 221);
    
    [self addSubview:lineView2];
    
    [self addSubview:self.saveBtn];
    
}

#pragma mark - 其它函数
-(void)click:(UIButton *)btn{
    if ([self.editViewDelegate respondsToSelector:@selector(clickEditViewBtn:)])
    {
        [self.editViewDelegate clickEditViewBtn:btn];
    }
}

-(void)clickImg{
    if ([self.editViewDelegate respondsToSelector:@selector(chooseHeaderImg)])
    {
        [self.editViewDelegate chooseHeaderImg];
    }
}
@end
